package com.example.jikeyoujikeyou.office_sz.view.base;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.comment.Global;
import com.example.jikeyoujikeyou.office_sz.comment.utils.ActivityTask;
import com.example.jikeyoujikeyou.office_sz.view.TitleBar;


public abstract class BaseActivity extends AppCompatActivity implements IView {

    AlertDialog.Builder builder;
    LinearLayout ll;
    TitleBar titleBar;

    /**
     * 当activity创建的时候将activity添加到ActivityTask
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //将Activity添加到任务栈
        ActivityTask.getInstance().addActivity(this);
        //设置activity界面布局（直接调用父类）
        super.setContentView(R.layout.activity_base);
        //初始化Global工具
        Global.init(this);
        //隐藏状态栏
        Global.setNoStatusBarFullMode(this);
        //隐藏标题栏
        getSupportActionBar().hide();
        //找到activity最外层线性布局
        ll = (LinearLayout) findViewById(R.id.ll);
        //找到titleBar
        titleBar = (TitleBar) findViewById(R.id.title_bar);
        //设置标题栏距离顶部距离
        Global.setStatusBarPadding(titleBar);
        //初始化弹出框
        builder = new AlertDialog.Builder(this);
    }

    /**
     * 重写setContentView
     * 将子类布局插入到titleBarde 的下面
     * @param layoutResID
     */
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        //获取到子类独有布局对象
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        //设置宽高以及权重
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
        //将属性赋值给view
        view.setLayoutParams(lp);
        //将view添加到最外层布局，TitleBar的下面
        ll.addView(view);
    }

    /**
     * 获取titlebar对象
     * @return
     */
    public TitleBar getTitleBar() {
        return titleBar;
    }



    public void fail(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                builder.setTitle("提示").setMessage(message).setPositiveButton("确定", null);
                builder.create().show();
            }
        });
    }

    /**
     * 重写finish方法。当activity销毁的时候移除ActivityTask列表
     */
    @Override
    public void finish() {
        super.finish();
        ActivityTask.getInstance().removeActivity(this);
    }

 }
